1. Field of the Invention
This invention relates to computer-based locks for synchronization, and particularly to effective lock reservation using cooperative multithreading and lightweight single reader reserved locks.
2. Description of Background
In programs written in programming languages such as Java®, there is often code that is synchronized, but synchronization is not required most of the time. Synchronization allows shared objects to be accessed by multiple objects or threads without corrupting the shared objects or the objects attempting to access the shared objects. Synchronized code typically requires special platform instructions to ensure memory coherency, which are usually expensive in terms of execution time and can significantly slow down a running program. Memory coherency instructions can halt execution of a program while queues or other structures that may be attempting to read or modify shared memory are emptied, preventing unexpected or out-of-order changes related to the shared memory. One compiler technique that has been proposed to solve this issue is lock reservation. With lock reservation, a thread reserves a shared object for itself unless the object is requested by another thread. Once a shared object is reserved, the reserving thread can enter and exit the synchronized region of code without executing the expensive memory coherency instructions.
When a thread wants access to an object that has been reserved for another thread, the reservation has to be cancelled, which is often a very expensive operation in terms of execution time. The reserving thread has to be stopped so that the object reservation can be cancelled. The cancellation operation can be extremely expensive if the threading model is preemptive, where thread execution can be stopped at any place, including the generated code for synchronized enter/exit primitives, e.g. Java® monitor-enter and monitor-exit primitives. Other lock reservation techniques with a smaller cost of cancellation have also been proposed; however, the gain of applying the optimization on modern processors is minimal. Identifying certain types of code patterns involved in code where synchronization is desired could further enhance synchronization performance. Therefore, it would be beneficial to develop methods to optimize synchronization techniques to reduce execution time delays associated with present lock reservation and cancellation algorithms. Accordingly, there is a need in the art for a method for effective lock reservation using cooperative multithreading, and further enhancements using lightweight single reader reserved locks.